<?php
/**
 * Search.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/12
 * Time: 14:21
 */
namespace app\api\open\member\address;

use app\api\open\member\Token;
use app\api\open\Base;
use app\api\model\MemberAddressModel;
use app\common\Singleton;

class Search extends Token
{
    protected $where = [];

    /**
     * Search constructor.
     * @param array $param
     * @throws \Exception
     */
    public function __construct($param = [])
    {
        parent::__construct($param);

		// 所在省
		if(isset($this->params['province']) && $this->params['province']!='')
		{
			$this->where['province'] = $this->params['province'];
		}
		// 所在市
		if(isset($this->params['city']) && $this->params['city']!='')
		{
			$this->where['city'] = $this->params['city'];
		}
		// 所在县/区
		if(isset($this->params['county']) && $this->params['county']!='')
		{
			$this->where['county'] = $this->params['county'];
		}
		// 会员ID
		if(isset($this->params['member_id']) && $this->params['member_id']!='')
		{
			$this->where['member_id'] = $this->params['member_id'];
		}
		// 经度
		if(isset($this->params['lng']) && $this->params['lng']!='')
		{
			$this->where['lng'] = $this->params['lng'];
		}
		// 纬度
		if(isset($this->params['lat']) && $this->params['lat']!='')
		{
			$this->where['lat'] = $this->params['lat'];
		}
		// 电话
		if(isset($this->params['tel']) && $this->params['tel']!='')
		{
			$this->where['tel'] = $this->params['tel'];
		}
		// 别名
		if(isset($this->params['alias']) && $this->params['alias']!='')
		{
			$this->where['alias'] = $this->params['alias'];
		}
		// 姓名
		if(isset($this->params['name']) && $this->params['name']!='')
		{
			$this->where['name'] = $this->params['name'];
		}
		// 详细地址
		if(isset($this->params['address']) && $this->params['address']!='')
		{
			$this->where['address'] = $this->params['address'];
		}
		// 是否默认地址（0否, 1是）
		if(isset($this->params['is_default']) && $this->params['is_default']!='')
		{
			$this->where['is_default'] = $this->params['is_default'];
		}
		// 是否删除（0否，1删除时间戳）
		if(isset($this->params['is_delete_time']) && $this->params['is_delete_time']!='')
		{
			$this->where['is_delete_time'] = $this->params['is_delete_time'];
		}
    }
    
    /**
     * @param array $param
     * @return array|false|null|\PDOStatement|string|\think\Model
     * @throws \Exception
     */
    public function index($param=[])
    {
        // 获取列表
        return MemberAddressModel::getInstance()->getList($this->where,[
            'field'=> '*'
        ]);
    }

    /**
     * @param array $param
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function all($param=[])
    {
        // 获取列表
        return MemberAddressModel::getInstance()->getAll($this->where,[
            'field' => '*',
            'order' => ['is_default'=>'desc','update_ts'=>'desc']
        ]);
    }
}